package 链表和树.二叉树;

import java.util.ArrayList;
import java.util.List;

//Java：二叉树的右视图
public class BinaryTreeRightSideView{
    public static void main(String[] args) {
        Solution solution = new BinaryTreeRightSideView().new Solution();
    }
    //leetcode submit region begin(Prohibit modification and deletion)
//    public class TreeNode {
//        int val;
//        TreeNode left;
//        TreeNode right;
//        TreeNode() {}
//        TreeNode(int val) { this.val = val; }
//        TreeNode(int val, TreeNode left, TreeNode right) {
//            this.val = val;
//            this.left = left;
//            this.right = right;
//        }
//    }
class Solution {
    public List<Integer> rightSideView(TreeNode root) {
        //最后的最后再把根节点算进去
        List<Integer> ans = new ArrayList<>();
        dfs(root, 0, ans);
        return ans;
    }
    private void dfs(TreeNode root, int depth, List<Integer>ans){
        if(root == null){
            return;
        }
        if(depth == ans.size()){
            ans.add(root.val);
        }

        dfs(root.right, depth +1, ans);
        dfs(root.left, depth +1, ans);
    }
}
//leetcode submit region end(Prohibit modification and deletion)

}